package com.zzr.demo.redis.service.impl;

import com.zzr.demo.redis.service.CounterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HyperLogLogOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
 * @author: xiaomi
 * @date: 2021/10/9
 * @description:
 */
@Service("UniqueCounterByHyperLogLog")
@Slf4j
public class UniqueCounterByHyperLogLogServiceImpl implements CounterService {

    final String UNIQUE_COUNTER_KEY = "unique::counter::hyperloglog";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean init(int value) {
        throw new NotImplementedException();
    }

    @Override
    public long increase() {
        HyperLogLogOperations<String, String> opsForHyperLogLog = stringRedisTemplate.opsForHyperLogLog();
        Long add = opsForHyperLogLog.add(UNIQUE_COUNTER_KEY, iptl.get());
        log.info("添加成功标志：{}", add);
        iptl.remove();
        return opsForHyperLogLog.size(UNIQUE_COUNTER_KEY);
    }

    @Override
    public long increase(int num) {
        throw new NotImplementedException();
    }

    @Override
    public long decrease() {
        throw new NotImplementedException();
    }

    @Override
    public long decrease(int num) {
        throw new NotImplementedException();
    }
}
